package com.totcy.atom.view.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.totcy.atom.R;
import com.totcy.atom.bean.User;
import com.totcy.atom.presenter.LoginPresenter;
import com.totcy.atom.presenter.contract.LoginContract;
import com.totcy.atom.utils.UserUtil;
import com.totcy.atom.utils.glide.GlideUtil;
import com.totcy.baselibrary.manage.AppManager;
import com.totcy.baselibrary.utils.SPUtil;
import com.totcy.baselibrary.utils.ToastUtil;
import com.totcy.baselibrary.widget.CircleImageView;

import org.datatist.sdk.DatatistSDK;
import org.datatist.sdk.Track;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnLongClick;

import static com.totcy.atom.view.fragment.HomeMineFragment.USER_IMG;

/**
 * description: 登录界面
 *
 * @author: Totcy
 * date: 2017/10/30 14:55
 * update: 2017/10/30
 */
public class LoginActivity extends BaseFullActivity<LoginContract.View, LoginPresenter> implements LoginContract.View {

    @BindView(R.id.account)
    EditText account;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.iv_userImg)
    CircleImageView userImg;

    @Override
    public boolean isChildLayoutBelowTitle() {
        return false;
    }

    @Override
    public String getTitleName() {
        return getString(R.string.action_sign_in);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        getToolbarTitleLayout().setNavigationIcon(null);
        setToolbarTitleLayoutBackground(R.color.transparent);

        if (!TextUtils.isEmpty((String) SPUtil.get(this, USER_IMG, ""))) {
            GlideUtil.loadImage(this, SPUtil.get(this, USER_IMG, ""), userImg, false);
        }
    }

    @OnLongClick({R.id.btn_login})
    public boolean onLongClick() {
        Random random = new Random();
        int id = random.nextInt(100000);
        onLoginResult(new User("游客" + id));

        return true;
    }

    @OnClick({R.id.btn_login, R.id.tvToRegister})
    public void onViewClicked(View v) {
        if (v.getId() == R.id.tvToRegister) {
            openActivityForResult(RegisterActivity.class, RegisterActivity.REQUEST_CODE_REGISTER);
            return;
        }
        if (TextUtils.isEmpty(account.getText())) {
            account.setError(getString(R.string.account_cannot_be_empty));
            return;
        } else if (TextUtils.isEmpty(password.getText())) {
            password.setError(getString(R.string.password_cannot_be_empty));
            return;
        } else {
            account.setError(null);
            password.setError(null);
        }

        //onLoginResult(new User(account.getText().toString()));

        getPresenter().doLogin(account.getText().toString(), password.getText().toString());
    }


    @Override
    public void onLoginResult(User user) {
        Map<String, String> params = new HashMap<>();
        params.put("loginType", "账号密码登录");
        Track.track().login().trackLogin(user.getUserName() + "", params).submit(DatatistSDK.getTracker());
        //保存用户信息
        UserUtil.getInstances().saveUserInfo(user);

        startActivity(new Intent(LoginActivity.this, MainActivity.class));
        finish();
    }

    @Override
    public void onLoginFail(String message) {
        showDialogTips(message);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK && requestCode == RegisterActivity.REQUEST_CODE_REGISTER) {
            assert data != null;
            onLoginResult((User) data.getSerializableExtra("userInfo"));
        }

    }
}

